package org.sky.common.web.util;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ScanPackageUtil {
	public static List<String> scanPackage(String packageName) {
		List<String> classNames = new ArrayList<String>();
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		String resourceName = packageName.replaceAll("\\.", "/");
		URL url = loader.getResource(resourceName);
		File urlFile=null;
		try {
			urlFile = new File(url.toURI());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		File[] files = urlFile.listFiles();
		for (File f : files)
			getClassName(packageName, f, classNames);
		return classNames;
	}
	
	private static void getClassName(String packageName, File packageFile,List<String> list) {
		if (packageFile.isFile()) {
			list.add(packageName + "." + packageFile.getName().replace(".class",""));
		} else {
			File[] files = packageFile.listFiles();
			String tmPackageName = packageName + "." + packageFile.getName();
			for (File f : files) {
				getClassName(tmPackageName, f, list);
			}
		}
	}
	 public static void main(String[] args) throws URISyntaxException {
		 List<String> list =ScanPackageUtil.scanPackage("com");
		 for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			System.out.println(string);
		}
	}
}
